﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;


namespace MVCWeb.Models
{
    public class CustomerModel
    {
        [Required]
        [DisplayName("Customer ID")]
        public int customer_id { get; set; }

        [Required]
        [DisplayName("Email")]
        public string email { get; set; }

        [Required]
        [DisplayName("First Name")]
        public string first_name { get; set; }

        [Required]
        [DisplayName("Last Name")]
        public string last_name { get; set; }

        [Required]
        [DisplayName("Street Address")]
        public string address { get; set; }

        [Required]
        [DisplayName("City")]
        public string city { get; set; }

        [Required]
        [DisplayName("State")]
        public string state { get; set; }

        [Required]
        [DisplayName("Zip")]
        public int zip { get; set; }

        [Required]
        [DisplayName("Permission")]
        public int permission { get; set; }

        [Required]
        [DisplayName("Block Status")]
        public int blockuser { get; set; }

        [DisplayName("Remember me?")]
        public bool RememberMe { get; set; }

    }


    public static class CustomerClientServices
    {
        //createcustomer
        public static void CreateCustomer(CustomerModel customer)
        {
            SLCustomer.BaseCustomer cust = DTO_to_SL(customer);
            SLCustomer.ISLCustomer SLCust = new SLCustomer.SLCustomerClient();

            string[] errors = new string[0];
            SLCust.InsertCustomer(cust, ref errors);
        }
        public static List<CustomerModel> GetName()
        {

            SLCustomer.ISLCustomer infor = new SLCustomer.SLCustomerClient();
            List<CustomerModel> inforList = new List<CustomerModel>();
            string[] erros = new string[0];

            SLCustomer.BaseCustomer[] users = infor.GetName(ref erros);

            foreach (SLCustomer.BaseCustomer b in users)
            {
                CustomerModel people = DTO_to_PL(b);
                inforList.Add(people);
            }
            return inforList;
        }

 

        //update residence
        public static void UpdateResidence(int id, CustomerModel customer)
        {
            SLCustomer.BaseCustomer cust = DTO_to_SL(customer);
            SLCustomer.ISLCustomer SLCust = new SLCustomer.SLCustomerClient();

            string[] errors = new string[0];
            SLCust.UpdateUserReden(id, cust, ref errors);
        }

        //get customer details
        public static CustomerModel GetCustomerDetails(int id)
        {
            SLCustomer.ISLCustomer SLCust = new SLCustomer.SLCustomerClient();
            string[] errors = new string[0];

            SLCustomer.BaseCustomer cust = SLCust.GetCustomerDetail(id, ref errors);

            return DTO_to_PL(cust);
        }

        //get id from email
        public static int GetIdFromEmail(string email)
        {
            SLCustomer.ISLCustomer SLCust = new SLCustomer.SLCustomerClient();
            string[] errors = new string[0];

            int id = SLCust.IdfromEmail(email, ref errors);

            return id;
        }

        public static CustomerModel GetLogInfor(String email, String first)
        {
            SLCustomer.ISLCustomer SLCust = new SLCustomer.SLCustomerClient();
            string[] errors = new string[0];

            if (email == null)
                email = "";

            SLCustomer.BaseCustomer bs = new SLCustomer.BaseCustomer();
            bs = SLCust.LoginInCheck(email, first, ref errors);

            return DTO_to_PL(bs);

        }

        //DTO to PL
        private static CustomerModel DTO_to_PL(SLCustomer.BaseCustomer customer)
        {
            CustomerModel PLCustomer = new Models.CustomerModel();
            PLCustomer.customer_id = customer.customer_id;
            PLCustomer.email = customer.email;
            PLCustomer.first_name = customer.first_name;
            PLCustomer.last_name = customer.last_name;
            PLCustomer.address = customer.address;
            PLCustomer.city = customer.city;
            PLCustomer.state = customer.state;
            PLCustomer.zip = customer.zip;
            PLCustomer.permission = customer.webmaster;
            PLCustomer.blockuser = customer.blockuser;

            return PLCustomer;
        }

        //DTO to SL
        private static SLCustomer.BaseCustomer DTO_to_SL(CustomerModel customer)
        {
            SLCustomer.BaseCustomer SLCust = new MVCWeb.SLCustomer.BaseCustomer();
            SLCust.customer_id = customer.customer_id;
            SLCust.email = customer.email;
            SLCust.first_name = customer.first_name;
            SLCust.last_name = customer.last_name;
            SLCust.address = customer.address;
            SLCust.city = customer.city;
            SLCust.state = customer.state;
            SLCust.zip = customer.zip;
            SLCust.webmaster = customer.permission;
            SLCust.blockuser = customer.blockuser;

            return SLCust;
        }

    }

}